package com.agile.leetcode.hutool.csv;

import cn.hutool.core.annotation.Alias;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;

import java.util.List;

/**
 * @Author：chenzhangkun
 * @Date：2023/11/1 13:53
 */
public class CsvDemo {
    public static void main(String[] args) {

        CsvWriter writer = CsvUtil.getWriter("D://czk.csv", CharsetUtil.CHARSET_UTF_8);

        writer.write(new String[]{"姓名","学号","年龄"},
                new String[]{"czk","12","25"},
                new String[]{"czj","45","88"});

        CsvReader reader = CsvUtil.getReader();

        List<Person> personList = reader.read(ResourceUtil.getUtf8Reader("D://czk.csv"), Person.class);
        System.out.println(personList);

        CsvData data = reader.read(FileUtil.file("D://czk.csv"));
        List<CsvRow> rows = data.getRows();
        for (CsvRow row : rows) {
            Console.log(row.getRawList());
        }
    }
    public static class Person{
        @Alias("姓名")
        private String name;
        @Alias("学号")
        private String number;
        @Alias("年龄")
        private Integer age;

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", number='" + number + '\'' +
                    ", age=" + age +
                    '}';
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }
}
